using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
namespace Data.FileManager
{
    public sealed class FileHandler
    {
        static readonly FileHandler instance = new FileHandler();
        static string workingDirectory;
        static FileHandler()
        {
        }

        private FileHandler()
        { 
        }

        public static FileHandler Instance
        {
            get
            {
                return instance;
            }
        }
        public static string WorkingDirectory
        {
            get
            {
                return workingDirectory;
            }

        }

        public StreamReader openFileForReading(string typeOfFile)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            StreamReader sReader;
            openFile.DefaultExt = "dat";
            openFile.Filter = "Data files (*.dat)|*.dat";
            openFile.InitialDirectory = System.Environment.SystemDirectory;
            openFile.Multiselect = false;
            openFile.Title = "Open File";
            openFile.InitialDirectory = getInitialDirectory();
            openFile.ShowDialog();
            if (openFile.FileName != null && openFile.FileName.Length != 0)
            {
                sReader = new StreamReader(openFile.FileName);
                workingDirectory = openFile.FileName.Substring(0,openFile.FileName.LastIndexOf('\\')+1);
            }
            else
                return null;
            if (typeOfFile.Equals(sReader.ReadLine()))
                return sReader;
            else
                return null;
        }
        public StreamReader ValidateFile(string typeOfFile, string fullPath)
        {
            StreamReader sReader = new StreamReader(fullPath, Encoding.ASCII);
            if (sReader.ReadLine() != typeOfFile)
                return null;
            return sReader;
        }

        public StreamWriter getFileToWrite(string typeOfFile, string fullPath)
        {
            StreamWriter sWriter = new StreamWriter(fullPath, false, Encoding.ASCII);
            sWriter.WriteLine(typeOfFile);
            sWriter.Flush();
            return sWriter;
        }
        private string getInitialDirectory()
        {
            string systemDir = System.Environment.SystemDirectory;
            StreamReader reader;
            try
            {
                reader = new StreamReader(systemDir + "\\myDef.ini");
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
            string fileContents = reader.ReadToEnd();
            int position = fileContents.IndexOf("install folder", 0);
            int stPosition = fileContents.IndexOf("=", position+15)+2;
            int laPosition = fileContents.IndexOf(System.Environment.NewLine, stPosition);
            string installFolder = fileContents.Substring(stPosition,laPosition-stPosition);
            installFolder.Trim();
            return installFolder;
        }
    }
}
